package com.yangfan.screenshot;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.os.IBinder;
import android.os.RemoteException;

import com.yangfan.screenshot.log.LogUtil;
import com.yangfan.screenshot.service.ScreenShotService;
import com.yangfan.screenshot.utils.ScreenUtil;

import java.util.Objects;

/**
 * @author : yangfan_rj
 * @date   : 2022/12/7-14:16
 * desc   : 截图入口类
 */
public class ScreenShotManager {

    private static final String TAG = ScreenShotManager.class.getSimpleName();

    private ScreenShotService mScreenShotService;
    private Context mContext;
    private static ScreenShotManager instance;
    /**
     * 截图状态回调
     */
    private ScreenShotStateCallback mScreenShotStateCallback;

    /**
     * 屏幕截图回调
     * onShotComplete和onShotBitmapList只需要实现一个即可
     *
     * @author yftech
     * @date 2022/12/13
     */
    public interface ScreenShotStateCallback {
        /**
         * 拼接完成后的bitmap
         *
         * @param bitmap 位图
         */
        void onShotCompleted(Bitmap bitmap);


        /**
         * 拼接进度
         *
         * @param index     完成索引
         * @param total 总计
         */
        void onStitchProgress(int index,int total);

        /**
         * 错误
         */
        void onError();
    }

    private ScreenShotManager(){

    }

    public static ScreenShotManager getInstance() {
        if (instance == null) {
            synchronized (ScreenShotManager.class) {
                instance = new ScreenShotManager();
            }
        }

        return instance;
    }

    /**
     * 初始化
     *
     * @param context 上下文
     */
    public static void init(Context context) {
        Objects.requireNonNull(context);
        LogUtil.init(context);
        getInstance().mContext = context;
        getInstance().bindService();
        ScreenUtil.instance.init(context);
    }

    /**
     * 完成屏幕截图
     *
     * @param screenShotStateCallback 屏幕截图回调
     */
    public void setScreenShotStateCallback(ScreenShotStateCallback screenShotStateCallback){
        mScreenShotStateCallback = screenShotStateCallback;

        if (mScreenShotService != null) {
            mScreenShotService.setScreenShotCallback(mScreenShotStateCallback);
        }
    }

    private IBinder.DeathRecipient mDeathRecipient = new IBinder.DeathRecipient() {
        @Override
        public void binderDied() {
            bindService();
        }
    };

    private ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            LogUtil.d(TAG,"====onServiceConnected iBinder = " + iBinder);
            try {
                iBinder.linkToDeath(mDeathRecipient,0);
            } catch (RemoteException e) {
                e.printStackTrace();
            }

            mScreenShotService = ((ScreenShotService.ScreenShotServiceBinder)iBinder).getService();
            mScreenShotService.setScreenShotCallback(mScreenShotStateCallback);
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            LogUtil.d(TAG,"====onServiceDisconnected");
        }
    };

    private void bindService() {
        LogUtil.d(TAG,"====bind FloatWindowService");
        Intent intent = new Intent(mContext, ScreenShotService.class);
        intent.setPackage(mContext.getPackageName());
        mContext.bindService(intent,mServiceConnection,Context.BIND_AUTO_CREATE);
    }

   public void unBindService(){
        mContext.unbindService(mServiceConnection);

    }

}
